﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using common;
using DAL;
using MS_Entity;

namespace MS_UI
{
    public partial class FrmLogin : Form
    {

        public FrmLogin()
        {
            InitializeComponent();
            //让label1，label2的父亲为pictureBox，否则背景图片的
            //透明色不显示
            this.label1.Parent = this.pictureBox1;
            this.label2.Parent = this.pictureBox1;
            this.anonymous.Parent = this.pictureBox1;
           

        }
        //登录判断
        private SysAdmins AdminLogin(SysAdmins objAdmins)
        {
            string sql = @"select LoginName,LoginPwd ,Role from sysAdmins where LoginName = '{0}' and LoginPwd = '{1}'";
            sql = string.Format(sql, objAdmins.LoginName,objAdmins.LoginPwd);
            //读取数据库中的表
            DataSet ds = SQLHelper.GetDataSet(sql);


          
            //如果临时表datatable的数据未来直接返回null
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach(DataRow i in ds.Tables[0].Rows)
                {

                   // 如果登录的账号，密码和数据库的对不上直接返回null
                    if (i["LoginName"] as string == objAdmins.LoginName &&
                       i["LoginPwd"] as string == objAdmins.LoginPwd
                        )
                    {


                        objAdmins.Role = i["Role"].GetType() == typeof(int)  ? 0 : (long)i["Role"];

                        return objAdmins;
                    }

          
                }



            }
   
                return null;
           
         
        }
          
        //登录按钮
        private void btn_login_Click(object sender, EventArgs e)
        {
            //是否匿名
            //是否勾选
            if (this.anonymous.Checked)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                //判断用户名和密码是否填写
                if (string.IsNullOrEmpty(this.user.Text.Trim()) ||
                    string.IsNullOrEmpty(this.password.Text.Trim())
                    )
                {
                    MessageBox.Show("用户名或密码不为空！");
                    return;
                }
                if (string.IsNullOrEmpty(this.user.Text.Trim()))
                {
                    //聚焦
                    this.user.Focus();
                    return;
                }
                else
                {
                    //判断用户名的长度是否符号要求
                    if (!DataValidate.IsUser(this.user.Text.Trim())){
                        MessageBox.Show("长度位1-6");
                        return;
                    }

                }
                if (string.IsNullOrEmpty(this.password.Text.Trim()))
                {
                    //聚焦
                    this.password.Focus();
                    return;
                }
                else
                {
                    //判断密码是否符号要求
                    if (!DataValidate.IsUser(this.user.Text.Trim()))
                    {
                        MessageBox.Show("长度为6-12的自然数");
                        return;
                    }
                }
                //登录
            var res =     AdminLogin(
                    new SysAdmins()
                    {
                      
                        LoginName = this.user.Text,
                        LoginPwd = this.password.Text,
                    }
                    );
                if(res == null)
                {
                    MessageBox.Show("用户名或者密码错误");
                    return;
                }
                else
                {

                    LoginInfo.sysAdmins = new SysAdmins()
                    {
   
                        LoginName = this.user.Text,
                        LoginPwd = this.password.Text,
                    };
                    if (AdminLogin(LoginInfo.sysAdmins) != null)
                    {
                        //执行跳转
                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "insert into sysAdmins (LoginName,LoginPwd) values ('niuma' , '123456')";
            SQLHelper.InsertData(sql);
        }

        private void anonymous_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btn_login_KeyDown(object sender, KeyEventArgs e)
        {
            //if(e.KeyCode == Keys.Enter)
            //{
            //    btn_login_Click(null,null);
            //}
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void btn_login_Clicks(object sender, EventArgs e)
        {

        }
    }
}
                

                
            
        
